package com.cug.campus_sh_market_server.entity.po

import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.extension.activerecord.Model
import com.cug.campus_sh_market_server.entity.myenum.IsDelEnum
import com.cug.campus_sh_market_server.util.UserContext
import com.fasterxml.jackson.annotation.JsonIgnore
import io.swagger.annotations.ApiModelProperty
import java.io.Serializable
import java.time.LocalDateTime

abstract class  BaseEntity(
    /**
     * 创建者
     */
    @ApiModelProperty(value = "创建者", hidden = true)
    @JsonIgnore
    @TableField(select = false)
    open var creator: Int? = null,

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间", hidden = true)
    @JsonIgnore
    @TableField(select = false)
    open var createdTime: LocalDateTime? = null
) : Model<BaseEntity>(), Serializable {


    /**
     * 修改者
     */
    @ApiModelProperty(value = "修改者", hidden = true)
    @JsonIgnore
    @TableField(select = false)
    open var modifier: Int? = null

    /**
     * 修改时间
     */
    @ApiModelProperty(value = "创建时间", hidden = true)
    @JsonIgnore
    @TableField(select = false)
    open var modifiedTime: LocalDateTime? = null

    /**
     * 是否删除
     */
    @TableField(select = false)
    @JsonIgnore
    @ApiModelProperty(value = "是否删除", hidden = true)
    open var del: Int? = null

    override fun toString(): String {
        return "creator=$creator, createTime=$createdTime, modifier=$modifier, modifyTime=$modifiedTime, isDel=$del"
    }

    /**
     * 插入数据前调用
     * @return BaseEntity
     */
    fun create(): BaseEntity {
        this.createdTime = LocalDateTime.now()
        this.del = IsDelEnum.NO.code
        this.creator = UserContext.get()?.id
        return this
    }

    /**
     * 修改数据前调用
     * @return BaseEntity
     */
    fun modify(): BaseEntity {
        this.modifiedTime = LocalDateTime.now()
        this.modifier = UserContext.get()?.id
        return this
    }
}
